package com.minggo.charmword.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.minggo.charmword.R;
import com.minggo.charmword.activity.WordStoreActivity;
import com.minggo.charmword.model.Word;

/**
 * 用户词的适配器
 * 
 * @author minggo
 * @date 2014-9-2 下午5:21:33
 */
public class WordStoreAdapter extends BaseAdapter {

	private List<Word> wordList;
	private Context context;
	private RemarkListener remarkListener;

	private int type = 0;

	public WordStoreAdapter(Context context, List<Word> wordList, RemarkListener remarkListener, int wordType) {
		this.wordList = wordList;
		this.context = context;
		this.remarkListener = remarkListener;
		this.type = wordType;
	}

	@Override
	public int getCount() {
		return wordList.size();
	}

	@Override
	public Object getItem(int position) {
		return wordList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_word_store, null);
			viewHolder.wordNameTv = (TextView) convertView.findViewById(R.id.tv_word_name);
			viewHolder.phoneticsTv = (TextView) convertView.findViewById(R.id.tv_word_phonetics);
			viewHolder.explainTv = (TextView) convertView.findViewById(R.id.tv_word_example);
			viewHolder.remarkRememberBt = (Button) convertView.findViewById(R.id.bt_add_to_remember);

			viewHolder.remarkRememberBt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					remarkListener.onRemark(position);
				}
			});

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (type == WordStoreActivity.NEW_WORD) {
			viewHolder.remarkRememberBt.setText(context.getString(R.string.add_to_remenber));
		} else if (type == WordStoreActivity.REMEMBER_WORD) {
			viewHolder.remarkRememberBt.setText(context.getString(R.string.remove_to_remenber));
		}

		viewHolder.wordNameTv.setText(wordList.get(position).word);
		viewHolder.phoneticsTv.setText(wordList.get(position).phonetics);
		viewHolder.explainTv.setText(wordList.get(position).explain);

		return convertView;
	}

	private class ViewHolder {
		TextView wordNameTv;
		TextView phoneticsTv;
		TextView explainTv;
		Button remarkRememberBt;
	}

	public interface RemarkListener {
		public void onRemark(int position);
	}
}
